﻿using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using X23.Application.SignalRManagement;

namespace X23.Application.NotificationManagement
{
    /// <summary>
    /// 通知中心
    /// </summary>
    public class NotificationManagementService : INotificationManagementService
    {
        private readonly IHubContext<NotificationHub> _hubContext;

        public NotificationManagementService(IHubContext<NotificationHub> hubContext)
        {
            _hubContext = hubContext;
        }

        /// <summary>
        /// 给全部用户发送消息
        /// </summary>
        /// <param name="method">方法</param>
        /// <param name="arg">消息内容</param>
        /// <returns></returns>
        public async Task SendAll(string method, object arg)
        {
            await _hubContext.Clients.All.SendAsync(method, arg);
        }
        /// <summary>
        /// 给用户发消息
        /// </summary>
        /// <param name="user">用户唯一标识（用户id）[new Claim(ClaimTypes.NameIdentifier, user.Id.ToString())]</param>
        /// <param name="method">方法</param>
        /// <param name="arg">消息内容</param>
        /// <returns></returns>
        public async Task SendUser(string user, string method, object arg)
        {
            await _hubContext.Clients.User(user).SendAsync(method, arg);
        }
        /// <summary>
        /// 给组发消息
        /// </summary>
        /// <param name="groupName">用户组</param>
        /// <param name="method">方法</param>
        /// <param name="arg">消息内容</param>
        /// <returns></returns>
        public async Task SendGroup(string groupName, string method, object arg)
        {
            await _hubContext.Clients.Group(groupName).SendAsync(method, arg);
        }
    }
}
